package com.mmlc.meimei.lichengkongjian.customview;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.meimei.lichengkongjian.been.CustomShowListView_Order1Bean;
import com.mmlc.meimei.lichengkongjian.been.ListItemBeanNew;
import com.mmlc.meimei.lichengkongjian.been.LogisticstBean;
import com.mmlc.meimei.lichengkongjian.been.OrderDetailBean;
import com.mmlc.meimei.lichengkongjian.been.RefundListBean;
import com.mmlc.meimei.lichengkongjian.classify.FastGoodsDetailActivity;
import com.mmlc.meimei.lichengkongjian.classify.ProductInfoActivity;
import com.mmlc.network.responsebean.OrderDetailResponseBean;
import com.mmlc.network.responsebean.OrderListResponseBean;

import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

public class MuchOrderItemGroup extends LinearLayout
{
	public MuchOrderItemGroup(Context context, AttributeSet attrs) {
		super(context, attrs);
		setOrientation(RadioGroup.VERTICAL);
	}


	public void addItem(List<OrderListResponseBean.DataBean.OrderGoodsBean> orderList){
		this.removeAllViews();
		for(int i=0;i<orderList.size();i++){
			View view = View.inflate(getContext(), R.layout.view_muchorderitemgroup, null);
			initView(orderList.get(i),view);
			addView(view);
		}
	}
	public void addOrder1(List<CustomShowListView_Order1Bean.ItemBean> orderList){
		/*this.removeAllViews();
		for(int i=0;i<orderList.size();i++){
			View view = View.inflate(getContext(), R.layout.view_order1_item, null);
			initOrder1Item(orderList.get(i),view);
			addView(view);
		}*/
	}

	public void addListItem_new(ListItemBeanNew bean){
		this.removeAllViews();
		List<ListItemBeanNew.ItemBean> orderList = bean.itemBeenList;
		for(int i=0;i<orderList.size();i++){
			View view = View.inflate(getContext(), R.layout.view_order1_item, null);
			initOrder1Item(orderList.get(i),view);
			addView(view);
		}
	}

	private void initOrder1Item(ListItemBeanNew.ItemBean itemBean, View view) {
		ImageView iv_photo = (ImageView) view.findViewById(R.id.iv_photo);
		x.image().bind(iv_photo,itemBean.imgUrl);

		TextView tv_firstTitle = (TextView) view.findViewById(R.id.tv_firstTitle);
		tv_firstTitle.setText(itemBean.firstTitle);

		TextView tv_secondTitletle = (TextView) view.findViewById(R.id.tv_secondTitletle);
		tv_secondTitletle.setText(itemBean.secondTitle);

		TextView tv_shopPrice = (TextView) view.findViewById(R.id.tv_shopPrice);
		tv_shopPrice.setText("¥ "+itemBean.shopPrice);

		TextView tv_shopCount = (TextView) view.findViewById(R.id.tv_shopCount);
		tv_shopCount.setText("x "+itemBean.shopCount);
	}

	public void addRefundItem(List<RefundListBean.ItemBean> orderList){
		this.removeAllViews();
		for(int i=0;i<orderList.size();i++){
			View view = View.inflate(getContext(), R.layout.view_muchorderitemgroup, null);
			initRefundItem(orderList.get(i),view);
			addView(view);
		}
	}

	private void initRefundItem(RefundListBean.ItemBean itemBean, View view) {
		ImageView iv_photo = (ImageView) view.findViewById(R.id.iv_photo);
		x.image().bind(iv_photo,itemBean.imgUrl);

		TextView tv_shopInfo = (TextView)view.findViewById(R.id.tv_shopInfo);
		tv_shopInfo.setText(itemBean.firstTitle);

		TextView tv_shopColor = (TextView)view.findViewById(R.id.tv_shopColor);
		tv_shopColor.setText(itemBean.secondTitle);

		TextView tv_shopMoney = (TextView)view.findViewById(R.id.tv_shopMoney);
		tv_shopMoney.setText("¥ "+ itemBean.shopPrice);

		TextView tv_count = (TextView)view.findViewById(R.id.tv_count);
		tv_count.setText("x"+ itemBean.shopCount);
	}

	public MuchButtonGroup addDetailItem(final ListItemBeanNew.ItemBean itemBean){
		this.removeAllViews();
		View view = View.inflate(getContext(), R.layout.view_muchorderitemgroup1, null);
		initOrder1Item(itemBean,view);

		addView(view);

		return (MuchButtonGroup) view.findViewById(R.id.mg_buttonGroup);
	}
	public MuchButtonGroup addDetailItemClicked(final ListItemBeanNew.ItemBean itemBean,final String zg_type){
		this.removeAllViews();
		View view = View.inflate(getContext(), R.layout.view_muchorderitemgroup1, null);
		view.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(zg_type.equals("3")){
					Intent intent = new Intent(getContext(), FastGoodsDetailActivity.class);
					intent.putExtra("id", itemBean.goodsId);
//				intent.putExtra("goods_id","13884");
					intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					getContext().startActivity(intent);
				}else {
					Intent intent = new Intent(getContext(), ProductInfoActivity.class);
					intent.putExtra("goods_id", itemBean.goodsId);
//				intent.putExtra("goods_id","13884");
					intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					getContext().startActivity(intent);
				}
			}
		});
		initOrder1Item(itemBean,view);

		addView(view);

		return (MuchButtonGroup) view.findViewById(R.id.mg_buttonGroup);
	}
	public void addLogisticsItem(List<LogisticstBean.ItemBean> itemBeen){
		this.removeAllViews();
		for(int i=0;i<itemBeen.size();i++){
			View view = View.inflate(getContext(), R.layout.adapter_logistics, null);
			LogisticstBean.ItemBean itemBean = itemBeen.get(i);
			initLogisticsBean(i,itemBean,view);
			addView(view);
		}
	}

	private void initLogisticsBean(int positioin, LogisticstBean.ItemBean itemBean, View view) {
		ImageView imageView = (ImageView) view.findViewById(R.id.iv_photo);
		x.image().bind(imageView,itemBean.imgUrl);

		TextView tv_shopInfo = (TextView) view.findViewById(R.id.tv_shopInfo);
		tv_shopInfo.setText(itemBean.firstTitle);

		TextView tv_shopColor = (TextView) view.findViewById(R.id.tv_shopColor);
		tv_shopColor.setText(itemBean.secondTitle);

		TextView tv_shopMoney = (TextView) view.findViewById(R.id.tv_shopMoney);
		tv_shopMoney.setText("¥"+itemBean.shopPrice);

		TextView tv_count = (TextView) view.findViewById(R.id.tv_count);
		tv_count.setText("x"+itemBean.shopCount);
	}

	private void initDetailItemBean(ListItemBeanNew.ItemBean itemBean,View view) {
	/*	ImageView iv_photo = (ImageView) view.findViewById(R.id.iv_photo);
		x.image().bind(iv_photo,itemBean.imgUrl;

		TextView tv_shopDesc = (TextView) view.findViewById(R.id.tv_shopDesc);
		tv_shopDesc.setText(itemBean.);

		TextView tv_color = (TextView) view.findViewById(R.id.tv_color);
		tv_color.setText(itemBean.getSpec_name());

		TextView tv_currentPrice = (TextView) view.findViewById(R.id.tv_currentPrice);
		tv_currentPrice.setText("¥ "+itemBean.getPrice());

		TextView tv_count = (TextView) view.findViewById(R.id.tv_count);
		tv_count.setText(itemBean.getNum());


		//动态添加展示按钮
		MuchButtonGroup mg_buttonGroup = (MuchButtonGroup) view.findViewById(R.id.mg_buttonGroup);
		mg_buttonGroup.addButton(calcButtonType(itemBean.getOper()));
		mg_buttonGroup.setMuchButtonClickedListener(new MuchButtonGroup.MuchButtonClickedListener() {
			@Override
			public void clicked(View v, int type) {
				OrderListUtils d = new OrderListUtils();
				d.doSomething(getOrderButtonBean(v,type));
			}

			private OrderButtonBean getOrderButtonBean(View v, int type) {
				OrderButtonBean buttonBean = new OrderButtonBean();
				buttonBean.rootView = rootView;
				buttonBean.activity = activity;
				buttonBean.itemPositon = position;
				buttonBean.orderId = orderId;
				buttonBean.goodsId = goodsId;
				buttonBean.buttonType = type;
				buttonBean.payType = payType;
				buttonBean.buttonView = v;
				buttonBean.from = OrderButtonBean.IGNORE;
				return buttonBean;
			}
		});*/

	}
	public List<Integer> calcButtonType(OrderDetailResponseBean.DataBean.OrderGoodsBean.OperBeanX oper) {
		List<Integer> orderButtonType = new ArrayList<Integer>();
		if(oper.getComment() == 1){//评价晒单 type = 4
			orderButtonType.add(4);
		}
		if(oper.getSale_after() == 1){//申请售后 type = 5
			orderButtonType.add(5);
		}
		if(oper.getBuy_again() == 1){//再次购买 type = 6
			orderButtonType.add(6);
		}
		return orderButtonType;
	}

	private void initView(OrderListResponseBean.DataBean.OrderGoodsBean order, View view) {
		processSingleOrder(order,view);
	}
	private void initView(OrderDetailBean.DetailItemBean itemBean, View view) {

	}

	private void processSingleOrder(OrderListResponseBean.DataBean.OrderGoodsBean bean, View view) {

		ImageView iv_photo = (ImageView) view.findViewById(R.id.iv_photo);
		x.image().bind(iv_photo,bean.getImg_thumb_min());

		TextView tv_shopInfo = (TextView)view.findViewById(R.id.tv_shopInfo);
		tv_shopInfo.setText(bean.getGoods_name());

		TextView tv_shopColor = (TextView)view.findViewById(R.id.tv_shopColor);
		tv_shopColor.setText(bean.getSpec_name());

		TextView tv_shopMoney = (TextView)view.findViewById(R.id.tv_shopMoney);
		tv_shopMoney.setText("¥ "+bean.getPrice());

		TextView tv_count = (TextView)view.findViewById(R.id.tv_count);
		tv_count.setText("x"+bean.getNum());
	}
}
